<template>
  <div class="goodsdetail">
    <Header />
    <div class="myswiper">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="item in gallery" :key="item.id">
          <img :src="item.img_url" alt="" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="header">
      <div>30天无忧退货</div>
      <div>48小时快速退款</div>
      <div>满88元免邮费</div>
    </div>
    <div class="goods-desc">
      <p class="name">{{ info.name }}</p>
      <p class="desc">{{ info.goods_brief }}</p>
      <p class="price">￥{{ info.retail_price }}</p>
      <div class="select" @click="show = !show">
        <span>请选择商品规格和数量</span>
        <van-icon name="arrow" size="20px" />
      </div>
    </div>
    <div class="popup">
      <van-popup
        v-model="show"
        position="bottom"
        :overlay-style="{ height: 'calc(100% - 50px)' }"
        closeable
        :style="{ height: '30%', bottom: '50px' }"
      >
        <div class="row">
          <img :src="info.primary_pic_url" alt="" />
          <div class="text">
            <p class="price">价格{{ info.retail_price }}元</p>
            <p>清选择数量</p>
          </div>
        </div>
        <div class="row">
          <div class="number">数量</div>
          <van-stepper v-model="number" />
        </div>
      </van-popup>
    </div>
    <div class="good-parameter">
      <h1>商品参数</h1>
      <div class="li" v-for="item in attribute" :key="item.id">
        <div class="name">{{ item.name }}</div>
        <div class="value van-ellipsis">{{ item.value }}</div>
      </div>
    </div>
    <div class="content" v-html="info.goods_desc"></div>
    <div class="btn-box">
      <div class="collect" @click="collect">
        <van-icon name="star-o" size="22" :color="isCollect ? 'red' : ''" />
      </div>
      <router-link :to="{ path: '/cart' }" tag="div" class="car"
        ><van-icon name="cart-o" size="22" :badge="allnumber"
      /></router-link>
      <div class="buy" @click="buyNow">立即购买</div>
      <div class="addCar" @click="addCar">加入购物车</div>
    </div>

    <div class="question-list">
      <div class="title">
        <van-divider
          :style="{ color: '#000', borderColor: '#000', fontSize: '12px' }"
          >常见问题</van-divider
        >
      </div>
      <ul>
        <li v-for="item in issue" :key="item.id">
          <div class="question">
            <div class="dot"></div>
            {{ item.question }}
          </div>
          <p class="answer">{{ item.answer }}</p>
        </li>
      </ul>
    </div>
    <div class="more">
      <div class="title">
        <div class="c">
          <van-divider
            :style="{ color: '#000', borderColor: '#000', fontSize: '12px' }"
            >大家都在看</van-divider
          >
        </div>
      </div>

      <div class="goods-list">
        <router-link
          :to="{ path: '/detail', query: { id: item.id } }"
          tag="div"
          class="item"
          v-for="item in productList"
          :key="item.id"
        >
          <img :src="item.list_pic_url" alt="" />
          <p>{{ item.name }}</p>
          <p class="price">￥{{ item.retail_price }}</p>
        </router-link>
      </div>
    </div>
  </div>
</template>
<script>
import { goodsDetail, collect, addCar, buyNow } from "@/api/goods";
import Header from "@/components/header/header";
import { Dialog } from "vant";
export default {
  components: {
    Header,
  },
  data() {
    return {
      gallery: [],
      info: {},
      attribute: {},
      issue: [],
      productList: [],
      allnumber: 0,
      number: 0,
      show: false,
      isCollect: false,
      openId: "",
    };
  },
  methods: {
    collect() {
      collect({ goodsId: this.info.id, openId: this.openId }).then((res) => {
        if (res.data == "success") {
          this.isCollect = !this.isCollect;
        }
        console.log(res);
      });
    },
    addCar() {
      if (this.show) {
        addCar({
          goodsId: this.info.id,
          number: this.number,
          openId: this.openId,
        }).then((res) => {
          if (res.data == "success") {
            this.allnumber += this.number;
          }
        });
      } else {
        this.show = true;
      }
    },
    buyNow() {
      if (this.show) {
        buyNow({
          allPrise: this.info.retail_price * this.number,
          goodsId: this.info.id,
          openId: this.openId,
        }).then((res) => {
          console.log(res);
        });
      } else {
        this.show = true;
      }
    },
  },
  created() {
    this.openId = localStorage.getItem("openId");
    let id = this.$route.query.id;
    goodsDetail({
      id: id,
      openId: this.openId,
    }).then((res) => {
      this.gallery = res.gallery;
      this.info = res.info;
      this.attribute = res.attribute;
      this.issue = res.issue;
      this.productList = res.productList;
      this.allnumber = res.allnumber;
      console.log(res);
    });
  },
};
</script>
<style lang="scss" scoped>
.goodsdetail {
  padding-bottom: 50px;
}
.myswiper {
  width: 375px;
  height: 375px;
  img {
    height: 100%;
    width: 100%;
  }
}
.header {
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
  margin-bottom: 20px;
  line-height: 40px;
}
.goods-desc {
  padding-top: 10px;
  background-color: #fff;
  text-align: center;
  color: #000;
  .name {
    font-size: 20px;
    margin-bottom: 30px;
  }
  .desc {
    font-size: 14px;
    color: #999999;
  }
  .price {
    color: #b4282d;
    font-size: 18px;
    padding-bottom: 20px;
  }
  .select {
    height: 60px;
    border-top: 1px solid #f4f4f4;
    background-color: #fff;
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    line-height: 60px;
  }
}
.good-parameter {
  margin-top: 5px;
  padding: 20px 20px;
  background-color: #fff;
  .li {
    background-color: #f7f7f7;
    margin: 8px;
    display: flex;
    font-size: 12px;
    height: 40px;
    line-height: 40px;
    padding-right: 5px;
    .name {
      color: #999;
      width: 60px;
    }
    .value {
      flex: 1;
    }
  }
}
.popup {
  .row {
    margin: 10px;
    display: flex;
    align-items: center;
    img {
      width: 90px;
      height: 90px;
      margin: 10px;
    }
    .price {
      color: #b4282d;
    }
  }
}
.btn-box {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 99999;
  background-color: #fff;
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid #f4f4f4;
  div {
    text-align: center;
    font-size: 12px;
    .van-icon {
      margin: 15px;
    }
  }
  div:nth-child(n + 1) {
    border-left: 1px solid #f4f4f4;
  }
  .buy {
    height: 100%;
    line-height: 50px;
    width: 30%;
  }
  .addCar {
    height: 100%;
    line-height: 50px;
    width: 30%;
    color: #fff;
    background-color: #b4282d;
  }
}
.question-list {
  background-color: #fff;
  padding: 10px;
  .title {
    width: 50%;
    margin: 0 auto;
  }
  li {
    font-size: 13px;
    margin-bottom: 10px;
    p {
      padding: 0;
      margin: 0;
    }
    .question {
      display: flex;
      align-items: center;
      .dot {
        font-size: 16px;
        width: 4px;
        height: 4px;
        background: #b4282d;
        border-radius: 50%;
        margin: 5px;
      }
    }
    .answer {
      color: #787878;
      line-height: 20px;
      padding: 5px 20px;
    }
  }
}
.more {
  margin-top: 20px;

  .title {
    padding-top: 10px;
    background-color: #fff;
    .c {
      background-color: #fff;
      width: 50%;
      height: 30px;
      margin: 0 auto;
    }
  }
  .goods-list {
    width: 375px;
    background-color: #f4f4f4;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .item {
      width: 186px;
      background-color: #fff;
      margin-bottom: 3px;
      img {
        width: 151px;
      }
    }
  }
}
</style>
<style lang="scss">
.content {
  margin-top: 5px;
  img {
    display: block;
    width: 100%;
  }
  p {
    padding: 0;
    margin: 0;
  }
}
</style>